/**
 * 菜单详情对话框
 */
var PermissionDlg = {
    menuData: {},
    appTree: null,
    parentTree: null,
    validateFields: {
        appName: {
            validators: {
                notEmpty: {
                    message: '应用名称不能为空'
                }
            }
        },
        name: {
            validators: {
                notEmpty: {
                    message: '权限名称不能为空'
                },
                stringLength: {
                    max: 45,
                    message: '请输入45位以内的权限名称'
                },
            }
        },
        parentName: {
            validators: {
                notEmpty: {
                    message: '父级名称不能为空'
                },
                callback: {
                    message: '父级名称不能选择自己',
                    callback:function(value, validator,$field){
                        var id = $("#id").val();
                        var parentId = $("#parentPermission").val();;
                        if (typeof id != "undefined" && id == parentId) {
                            return false;
                        } else {
                            return true;
                        }
                    }
                }
            }
        },
        isMenu: {
            validators: {
                notEmpty: {
                    message: '是否是菜单不能为空'
                }
            }
        },
        url: {
            validators: {
                notEmpty: {
                    message: '请求地址不能为空'
                },
                stringLength: {
                    max: 45,
                    message: '请输入45位以为的请求地址'
                }
            }
        },
        num: {
            validators: {
                notEmpty: {
                    message: '排序不能为空'
                }
            }
        }
    }
};

/**
 * 清除数据
 */
PermissionDlg.clearData = function () {
    this.menuData = {};
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
PermissionDlg.set = function (key, val) {
    this.menuData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
}

/**
 * 获取对话框中的数据
 *
 * @param key 数据的名称
 */
PermissionDlg.get = function (key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
PermissionDlg.close = function () {
    parent.layer.close(window.parent.Permission.layerIndex);
}

/**
 * 收集数据
 */
PermissionDlg.collectData = function () {
    this.set('id').set('appId').set('name').set('parentId').set("isMenu").set('url').set('num').set('icon');
}

/**
 * 验证数据是否为空
 */
PermissionDlg.validate = function () {
    var bootstrapValidator = $("#menuForm").data('bootstrapValidator');
    bootstrapValidator.resetForm();
    bootstrapValidator.validate();
    return bootstrapValidator.isValid();
}

/**
 * 提交添加用户
 */
PermissionDlg.addPermissionSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Common.ctxPath + "/permission/add", function (result) {
        if (result.success) {
            Common.success(result.message + "!");
            window.parent.Permission.table.refresh();
            PermissionDlg.close();
        } else {
            Common.error(result.message + "!");
        }
    }, function (result) {
        Common.error(result.responseJSON.message + "!");
    });
    ajax.set(this.menuData);
    ajax.start();
}

/**
 * 提交修改
 */
PermissionDlg.editPermissionSubmit = function () {
    this.clearData();
    this.collectData();
    if (!this.validate()) {
        return;
    }
    //提交信息
    var ajax = new $ax(Common.ctxPath + "/permission/update", function (result) {
        if (result.success) {
            Common.success(result.message + "!");
            window.parent.Permission.table.refresh();
            PermissionDlg.close();
        } else {
            Common.error("修改失败!" + result.message + "!");
        }
    }, function (data) {
        Common.error("修改异常!" + data.responseJSON.message + "!");
    });
    ajax.set(this.menuData);
    ajax.start();
}

/**
 * 点击应用input框时
 */
PermissionDlg.onClickApp = function (e, treeId, treeNode) {
    $("#appName").val(PermissionDlg.appTree.getSelectedVal());
    $("#appId").val(treeNode.id);
};

/**
 * 双击应用input框
 */
PermissionDlg.onDblClickApp = function (e, treeId, treeNode) {
    $("#appName").val(PermissionDlg.appTree.getSelectedVal());
    $("#appId").val(treeNode.id);
    $("#appContent").fadeOut("fast");
};

/**
 * 显示应用权限选择的树
 */
PermissionDlg.showAppSelectTree = function () {
    Common.showInputTree("appName", "appContent");
};

/**
 * 点击父级权限input框时
 */
PermissionDlg.onClickParentName = function (e, treeId, treeNode) {
    $("#parentName").val(PermissionDlg.parentTree.getSelectedVal());
    $("#parentId").val(treeNode.id);
};

/**
 * 双击父级权限input框
 */
PermissionDlg.onDblClickParentName = function (e, treeId, treeNode) {
    $("#parentName").val(PermissionDlg.parentTree.getSelectedVal());
    $("#parentId").val(treeNode.id);
    $("#parentContent").fadeOut("fast");
};

/**
 * 显示父级权限选择的树
 */
PermissionDlg.showParentNameSelectTree = function () {
    var appId = $("#appId").val();
    if (appId == undefined || appId == null || appId == '') {
        Common.error("请选择添加权限的应用名称");
        return;
    }
    var parentTree = new $ZTree("parentTree", "/tree/permission/app/" + appId);
    parentTree.bindOnClick(PermissionDlg.onClickParentName);
    parentTree.bindOnDblClick(PermissionDlg.onDblClickParentName)
    parentTree.init();
    PermissionDlg.parentTree = parentTree;
    Common.showInputTree("parentName", "parentContent");
};

$(function () {
    Common.initValidator("menuForm", PermissionDlg.validateFields);
    var appTree = new $ZTree("appTree", "/tree/app");
    appTree.bindOnClick(PermissionDlg.onClickApp);
    appTree.bindOnDblClick(PermissionDlg.onDblClickApp)
    appTree.init();
    PermissionDlg.appTree = appTree;
});
